//
//  XLTalkedPeopleCell.m
//  XiangLove
//
//  Created by feng song on 2017/12/20.
//  Copyright © 2017年 BAOMIHUA.COM. All rights reserved.
//

#import "XLTalkedPeopleCell.h"
#import "NSDate+Ext.h"

@implementation XLTalkedPeopleCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self addLoadViews];
    }
    return self;
    
}

-(void)addLoadViews
{
    UIImageView * headView = [[UIImageView alloc] init];
    headView.clipsToBounds = YES;
    headView.layer.cornerRadius = 24;
    headView.contentMode = UIViewContentModeScaleAspectFill;
    headView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [headView setContentScaleFactor:[[UIScreen mainScreen] scale]];
    [self.contentView addSubview:headView];
    self.headView = headView;
    [headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(48, 48));
        make.left.equalTo(self.contentView.mas_left).offset(15);
        make.centerY.equalTo(self.contentView);
    }];
    
    UILabel * nameView = [[UILabel alloc] init];
    nameView.textColor = kBlackColor;
    nameView.font = kSetFont(17.0);
    [self.contentView addSubview:nameView];
    self.nameView = nameView;
    [self.nameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(12);
        make.left.equalTo(self.headView.mas_right).offset(10);
    }];
    
    UILabel * addressView = [[UILabel alloc] init];
    addressView.textColor = RGB_HEX(0x9395a2);
    addressView.font = kSetFont(13.0);
    [self.contentView addSubview:addressView];
    self.addressView = addressView;
    [self.addressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameView.mas_bottom).offset(3);
        make.left.equalTo(self.nameView.mas_left);
    }];
    
    UILabel * talkedView = [[UILabel alloc] init];
    talkedView.textColor = RGB_HEX(0x909090);
    talkedView.font      = kSetFont(10.0);
    [self.contentView addSubview:talkedView];
    self.talkedTime = talkedView;
    [self.talkedTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(18);
        make.right.equalTo(self.contentView.mas_right).offset(-15);
    }];
    
    
    UIView * bgLine = [[UIView alloc] initWithFrame:CGRectMake(73,72-0.5,XQScreenW-73,0.5)];
    bgLine.backgroundColor = RGB_HEX(0xeeeeee);
    [self.contentView addSubview:bgLine];
    
}


-(void)setDataModels:(XLTalkedPeopleModle *)dataModels
{
    if (_dataModels != dataModels) {
        _dataModels = dataModels;
    }
    
    [self.headView sd_setImageWithURL:[NSURL URLWithString:dataModels.HeadImg] placeholderImage:[UIImage imageNamed:@""]];
    self.nameView.text = dataModels.NickName;
    self.addressView.text = dataModels.HostCity;
    
    if (self.talkedTime) {
        if (dataModels.ChatTime.length > 0) {
            
            NSString * talkedStr =  [dataModels.ChatTime stringByReplacingOccurrencesOfString:@"T" withString:@" "];
            
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            //指定时间显示样式: HH表示24小时制 hh表示12小时制
            [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
            NSString *lastTime = talkedStr;
            NSDate *lastDate = [formatter dateFromString:lastTime];
            //以 1970/01/01 GMT为基准，得到lastDate的时间戳
            long firstStamp = [lastDate timeIntervalSince1970];
            NSLog(@"firstStamp:%ld",firstStamp);
            
            self.talkedTime.text = [lastDate shortTimeDescriptionOfDate];
            
        }else{
            
            [self.talkedTime mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentView.mas_top).width.offset(18);
                make.right.equalTo(self.contentView.mas_right).width.offset(15);
                make.size.mas_equalTo(CGSizeMake(0, 0));
            }];
            
            self.talkedTime.text = @"";
            
        }
        
    }
}


@end
